package org.kisekiproject.course {
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import org.kisekiproject.events.KisekiEvent;
	import org.kisekiproject.instance.Instance;

	/**
	 * A course node.
	 */
	public class CoursePage extends EventDispatcher {

		private var _instance:Instance;
		private var _displayObject:DisplayObject;
		private var _course:CourseModel;

		/**
		 * Constructor.
		 */
		public function CoursePage(instance:Instance) {
			_instance=instance;
			_instance.addEventListener(Event.COMPLETE,onInstanceComplete);
			_instance.addEventListener(KisekiEvent.OUTPUT_CHANGE,onOutputChange);
		}

		/**
		 * Output change.
		 */
		protected function onOutputChange(event:Event):void {
			dispatchEvent(new KisekiEvent(KisekiEvent.OUTPUT_CHANGE));
		}
		
		/**
		 * Instance complete.
		 */
		protected function onInstanceComplete(event:Event):void {
			dispatchEvent(new Event(Event.COMPLETE));
		}

		/**
		 * Set course reference.
		 */
		public function set course(value:CourseModel):void {
			_course=value;
		}

		/**
		 * Get display object.
		 */
		public function get displayObject():DisplayObject {
			if (!_displayObject) {
				_displayObject=DisplayObject(_instance.object);

				if (!_displayObject)
					throw new Error("Not a display object: "+_instance.definition.fullName);
			}
	
			return _displayObject;
		}

		/**
		 * Get id.
		 */
		public function get id():String {
			return _instance.id;
		}

		/**
		 * Set visibility.
		 */
		public function set visible(value:Boolean):void {
			_displayObject.visible=value;
		}

		/**
		 * Get visibility.
		 */
		public function get visible():Boolean {
			if (!_displayObject)
				return false;

			return _displayObject.visible;
		}
	}
}